﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System.Diagnostics;

namespace MyRTS
{
    public partial class Form1 : Form
    {


        List<Vector3> VertexL = new List<Vector3>();
        List<int> VertexIndexList = new List<int>();
        List<Vector3> Normals = new List<Vector3>();
        fbx f = null;
        DataTable table1 = new DataTable();
        scena scene;
        NewScene newscene;

        public Form1()
        {
            InitializeComponent();
            table1.Columns.Add("1");
            table1.Columns.Add("2");
            table1.Columns.Add("3");
            table1.Columns.Add("4");
            dataGridView1.DataSource = table1;
            for (int i = 0; i < 4; i++)
            {
                dataGridView1.Columns[i].Width = 50;
            }
            sectim = 1000 / (float)timer1.Interval;
            timer1.Start();
        }


        Matrix4 rotation = Matrix4.Identity;
        private void button1_Click(object sender, EventArgs e)
        {
            // Show the dialog and get result.
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK) // Test result.
            {
                string file = openFileDialog1.FileName;
                f = new fbx(file);
                if (newscene == null)
                {
                    newscene = new NewScene(f, shaderProgramHandle);
                }

                if (scene == null)
                {
                    scene = new scena(f, shaderProgramHandle);
                    
                }
                else
                {
                    scene.Add(f);
                }
                List<string> typelist = new List<string>();
                foreach (Model m in scene.ModelsList)
                {
                    consoleControl1.WriteOutput("Model -" + m.name, Color.GreenYellow);
                    consoleControl1.WriteOutput(Environment.NewLine, Color.GreenYellow);

                    foreach (string[] l in m.Properties)
                    {
                        try
                        {
                            if (typelist.Contains(l[1]))
                            {
                            }
                            else
                            {
                                typelist.Add(l[1]);
                            }
                        }
                        catch
                        {

                        }
                    }
                  
                    foreach (string s in m.propertiesname)
                    {
                        consoleControl1.WriteOutput(s, Color.White);
                        consoleControl1.WriteOutput(Environment.NewLine, Color.GreenYellow);
                    }
                  
                }

                List<MyRTS.Node> li = new List<Node>();
                
                List<string> lis = new List<string>();
                foreach (Node n in f.NodeList)
                {
                    lis.Add(n.name);
                }

                foreach (string s in lis)
                {
                  //  listBox1.Items.Add(s);
                }
                List<Node> mk = new List<Node>();
                TreeNode mkk = new TreeNode();
                mkk.Text = file + "   "+(f.file.Count()/1024)+" kB";
                foreach (Node n in f.NodeList)
                {
                    n.zwrocN(mk);
                    n.zwroc3(mkk);
                    
                }
                treeView1.Nodes.Add(mkk);
               // treeView1.DrawMode=TreeViewDrawMode.
           //     treeView1.ExpandAll();



                
               
                List<string> listaw = new List<string>();
                try
                {
                    foreach (Node n in mk)
                    {
                        //  listBox1.Items.Add(n.name);
                        listaw.Add(n.name);
                        //     listBox1.Items.Add("   ->" + n.PropertyList.Count.ToString()+"    Type->");
                        listaw.Add("   ->" + n.PropertyList.Count.ToString() + "    Type->");
                        foreach (Property p in n.PropertyList)
                        {
                            //   listBox1.Items.Add("    ---Type->"+p.type+"  Value= "+p.Datas);
                            listaw.Add("    ---Type->" + p.type + "  Value= " + p.Datas);
                        }
                        if (n.name == "Vertices")
                        {
                            byte[] h = n.PropertyList[0].Data;
                            int coile = 4;
                            if (n.PropertyList[0].type == "d")
                            {
                                coile = 8;
                            }
                            List<double> templ = new List<double>();
                            for (int i = 0; i < h.Length; i = i + coile)
                            {
                                byte[] tem = new byte[coile];
                                for (int j = 0; j < coile; j++)
                                {
                                    tem[j] = h[i + j];
                                }
                                templ.Add(BitConverter.ToDouble(tem, 0));

                            }
                            for (int i = 0; i < templ.Count; i = i + 3)
                            {
                                try
                                {
                                    VertexL.Add(new Vector3((float)templ[i], (float)templ[i + 1], (float)templ[i + 2]));
                                }
                                catch
                                {
                                }
                            }
                            ;
                        }
                        if (n.name == "PolygonVertexIndex")
                        {
                            byte[] h = n.PropertyList[0].Data;
                            int tem = 0;
                            for (int i = 0; i < h.Length; i = i + 4)
                            {
                                byte[] temb = new byte[4];
                                for (int j = 0; j < 4; j++)
                                {
                                    temb[j] = h[i + j];
                                }
                                int hj = BitConverter.ToInt32(temb, 0);
                                if (hj < 0)
                                {
                                    hj = -(hj + 1);
                                }
                                VertexIndexList.Add(hj);

                            }

                            ;
                        }
                        if (n.name == "Normals")
                        {
                            byte[] h = n.PropertyList[0].Data;
                            int coile = 4;
                            if (n.PropertyList[0].type == "d")
                            {
                                coile = 8;
                            }
                            List<double> templ = new List<double>();
                            for (int i = 0; i < h.Length; i = i + coile)
                            {
                                byte[] tem = new byte[coile];
                                for (int j = 0; j < coile; j++)
                                {
                                    tem[j] = h[i + j];
                                }
                                templ.Add(BitConverter.ToDouble(tem, 0));

                            }
                            for (int i = 0; i < templ.Count; i = i + 3)
                            {
                                Normals.Add(new Vector3((float)templ[i], (float)templ[i + 1], (float)templ[i + 2]));
                            }
                        }
                    }
                }
                catch
                {
                }

                int yuo = 0;

            }

        }
        int count = 0;
        float sectim = 0;
        Vector3 campospos = new Vector3(0, 0, 0);
        Vector3 target = new Vector3(0, 0, -1);
        float xangle = 0;
        float yangle = 0;
        float zangle = 0;
        float rotationspeed = 0.03f;
        float mousesens = 500;
        
        private void timer1_Tick(object sender, EventArgs e)
        {
            sw.Stop();
            double timeslice = sw.Elapsed.TotalMilliseconds;
            sw.Reset();
            sw.Start();



            float deltaRotation = (float)timeslice / 2000.0f;
         //   rotation += deltaRotation;
            glControl1.Invalidate();
            if (count > sectim)
            {
                label1.Text = sectim.ToString(".0");
                count = 0;
            }
            
            count++;

            rotation = Matrix4.CreateRotationY((float)deltaRotation);
 
        }

        private void glControl1_KeyDown(object sender, KeyEventArgs e)
        {

            Vector3 forward = new Vector3(viewMatrix.Row2);
          //  forward = Vector3.Normalize(forward);
            Vector3  right = new Vector3(viewMatrix.Row0);
          
            right = Vector3.Normalize(right);
            float alfa = 0.1f;
            if (e.KeyCode == Keys.W)
            {
                camposp.X += camtarget.X;
                camposp.Y += camtarget.Y;
                camposp.Z += camtarget.Z;
                campospos -= forward;
                
           //     pos = pos + forward;
            }
            if (e.KeyCode == Keys.S)
            {
                camposp.X -= camtarget.X;
                camposp.Y -= camtarget.Y;
                camposp.Z -= camtarget.Z;
                campospos += forward;
           //     pos = pos - forward;
            }
            if (e.KeyCode == Keys.D)
            {
                campospos += right;
               // camright = rotate(camright.X, camright.Y, camright.Z, camup.X, camup.Y, camup.Z, rotationspeed);
               // camtarget = rotate(camtarget.X, camtarget.Y, camtarget.Z, camup.X, camup.Y, camup.Z, rotationspeed);
                camposp += camright;
                
            
            }
            if (e.KeyCode == Keys.A)
            {

              //  camright = rotate(camright.X, camright.Y, camright.Z, camup.X, camup.Y, camup.Z, rotationspeed);
              //  camtarget = rotate(camtarget.X, camtarget.Y, camtarget.Z, camup.X, camup.Y, camup.Z, rotationspeed);
                camposp -= camright;
            }

          //  viewMatrix = Matrix4.LookAt(pos, pos + target, new Vector3(0, 1, 0));
            Vector4 tem = new Vector4(campospos, 1);
         //   viewMatrix.Row3 = tem;
            campos.Row3 = tem;




        }
        int oldx = 0;
        int oldy = 0;
        Matrix4 camrot = Matrix4.Identity;
        Matrix4 campos = Matrix4.Identity;
        Vector3 camposp = new Vector3(0, 0, 0);
        Vector3 camtarget = new Vector3(0, 0, -1);
        Vector3 camup = new Vector3(0, 1, 0);
        Vector3 camright = new Vector3(1, 0, 0);
       
        private void glControl1_MouseMove(object sender, MouseEventArgs e)
        {
            
            //float diff = ((float)e.X)-((float)glControl1.Width)/2;
            float diff = (float)oldx - (float)e.X;
            float diff2 = (float)oldy - (float)e.Y;
          
            //if
           // float yangle = diff;
         //   float xangle = diff2;
             float alfa = (float)Math.Atan(diff);
             float beta = (float)Math.Atan(diff2);
             camtarget = rotate(camtarget.X, camtarget.Y, camtarget.Z, camright.X, camright.Y, camright.Z, diff2 / mousesens);

             camright = rotate(camright.X, camright.Y, camright.Z, camup.X, camup.Y, camup.Z, diff / mousesens);
             camtarget = rotate(camtarget.X, camtarget.Y, camtarget.Z, camup.X, camup.Y, camup.Z, diff / mousesens);
             label2.Text = diff.ToString() + "    " + e.X.ToString();
             oldx = e.X;
             oldy = e.Y;
   
        }

        public Vector3 rotate(float vx, float vy, float vz, float ax, float ay, float az, float angle)
            {
          double ca = Math.Cos(angle);
          double sa = Math.Sin(angle);
          double crossx = -vy*az + vz*ay;
          double crossy = -vz*ax + vx*az;
          double crossz = -vx*ay + vy*ax;
          double dot = ax*vx + ay*vy + az*vz;
          double rx = vx*ca + crossx*sa + dot*ax*(1-ca);
          double ry = vy*ca + crossy*sa + dot*ay*(1-ca);
          double rz = vz*ca + crossz*sa + dot*az*(1-ca);
            
          vx = (float)rx; 
          vy = (float)ry; 
          vz = (float)rz;
          Vector3 Outv = new Vector3((float)vx, (float)vy, (float)vz);
          return Outv;
         }


        public static int zwrocdlugosc(string type)
        {
            int dlugosc = 0;
            if (type == "int"||type=="double"||type=="bool"||type=="enum"||type=="object"||type=="visibility")
                dlugosc = 1;
            if (type == "Vector3D"||type=="Lcl Transition"||type=="Lcl Rotation"||type=="Lcl Scaling"||type=="ColorRGB")
                dlugosc = 3;



            return dlugosc;
        }



    }

    
}
